package com.petdog.module.recognition.service.impl;

import com.petdog.infrastructure.entity.BreedInfo;
import com.petdog.infrastructure.mapper.BreedInfoMapper;
import com.petdog.module.recognition.service.BreedInfoService;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 犬种信息服务实现类
 * 提供犬种科普信息的查询和管理功能
 */
@Service
@RequiredArgsConstructor
public class BreedInfoServiceImpl implements BreedInfoService {
    private static final Logger log = LoggerFactory.getLogger(BreedInfoService.class);
    private final BreedInfoMapper breedInfoMapper;

    /**
     * 根据犬种ID获取犬种信息
     */
    public BreedInfo getBreedById(Integer breedId) {
        if (breedId == null || breedId <= 0) {
            log.warn("无效的犬种ID: {}", breedId);
            return null;
        }
        
        try {
            return breedInfoMapper.selectById(breedId);
        } catch (Exception e) {
            log.error("根据ID查询犬种信息失败: {}", breedId, e);
            return null;
        }
    }

    /**
     * 根据犬种名称获取犬种信息
     */
    public BreedInfo getBreedByName(String breedName) {
        if (breedName == null || breedName.trim().isEmpty()) {
            log.warn("犬种名称不能为空");
            return null;
        }
        
        try {
            return breedInfoMapper.selectByName(breedName);
        } catch (Exception e) {
            log.error("根据名称查询犬种信息失败: {}", breedName, e);
            return null;
        }
    }

    /**
     * 获取犬种ID通过犬种名称
     */
    public Long getBreedIdByName(String breedName) {
        BreedInfo breedInfo = getBreedByName(breedName);
        return breedInfo != null ? breedInfo.getBreedId().longValue() : null;
    }

    /**
     * 获取所有犬种信息列表
     */
    public List<BreedInfo> getAllBreeds() {
        try {
            return breedInfoMapper.selectAll();
        } catch (Exception e) {
            log.error("获取所有犬种信息失败", e);
            return null;
        }
    }

    /**
     * 保存犬种信息
     */
    public boolean saveBreed(BreedInfo breedInfo) {
        if (breedInfo == null) {
            log.warn("犬种信息不能为空");
            return false;
        }
        
        try {
            if (breedInfo.getBreedId() != null && breedInfo.getBreedId() > 0) {
                // 更新现有犬种信息
                return breedInfoMapper.update(breedInfo) > 0;
            } else {
                // 插入新犬种信息
                return breedInfoMapper.insert(breedInfo) > 0;
            }
        } catch (Exception e) {
            log.error("保存犬种信息失败", e);
            return false;
        }
    }

    /**
     * 删除犬种信息
     */
    public boolean deleteBreed(Integer breedId) {
        if (breedId == null || breedId <= 0) {
            log.warn("无效的犬种ID: {}", breedId);
            return false;
        }
        
        try {
            return breedInfoMapper.deleteById(breedId) > 0;
        } catch (Exception e) {
            log.error("删除犬种信息失败: {}", breedId, e);
            return false;
        }
    }
}
